import { StorehouseService } from './../../../@core/services/storehouse/storehouse.service';
import { Component, Input } from '@angular/core';
import { TableWidthConfig } from 'ng-devui';
import { DeviceService } from 'src/app/@core/services/device.ts/device.service';
import { MaterialService } from 'src/app/@core/services/material/material.service';
@Component({
  selector: 'app-select-storehouse',
  templateUrl: './select-storehouse.component.html',
  styleUrls: ['./select-storehouse.component.scss']
})
export class SelectStorehouseComponent {
  @Input() data: any;
  isSelected = false;
  displayTableData: any[] = [];
  displayTreeData: any[];
  treeData: any;
  filteredTreeNodes: any
  option: any;//
  tableheight = '230px';
  selectData: any;
  searchName: ''
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  pageSizeOptions = [10, 20, 30, 50, 100];
  dataTableOptions = {
    columns: [
      {
        field: 'radio',
        header: '选择',
        fieldType: 'radio',
        fixedLeft: '0px'
      },
      {
        field: 'code',
        header: '编码',
        fieldType: 'text',
        fixedLeft: '50px'
      },
      {
        field: 'name',
        header: '名称',
        fieldType: 'text',
        fixedLeft: '150px'
      },
      {
        field: 'name',
        header: '是否允许负库存',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '状态',
        fieldType: 'text',
      },
    ]
  };
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'radio',
      width: '50px',
    },
    {
      field: 'code',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    },
    {
      field: 'name',
      width: '100px'
    },
  ]
  searchMaterialCategoryValue: any;
  partClassId: any = null;
  searchWarehouseCategoryValue: any;
  constructor(private StorehouseService: StorehouseService) { }
  ngOnInit(): void {
    this.StorehouseService.getWarehouseCategorysWithChildren(1, 999).subscribe((res: any) => {
      this.treeData = res.items
      this.displayTreeData = [{ warehouseCategoryName: '全部', warehouseCategoryId: null }, ...this.treeData];
    })
    this.StorehouseService.getWarehouses(this.partClassId, null, this.searchName, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.displayTableData = data.items
      this.pager.total = data.totalCount
    })
  }
  radio(event: any, rowItem: any) {
    if (event.target.checked) {
      this.isSelected = true;
      this.selectData = rowItem
      console.log(this.selectData)
    }
  }
  pageIndexChange($event: any) {
    this.pager.pageIndex = $event;
    this.StorehouseService.getWarehouses(this.partClassId, null, this.searchName, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.displayTableData = data.items
      this.pager.total = data.totalCount
    })
  }//切换页码
  pageSizeChange($event: any) {
    this.StorehouseService.getWarehouses(this.partClassId, null, this.searchName, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.displayTableData = data.items
      this.pager.total = data.totalCount
    })
  }//改变每页显示数量
  search() {
    this.StorehouseService.getWarehouses(this.partClassId, null, this.searchName, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.displayTableData = data.items
      this.pager.total = data.totalCount
    })
  }
  search2() {
    this.pager.pageIndex = 1
    this.StorehouseService.getWarehouses(this.partClassId, null, this.searchName, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.displayTableData = data.items
      this.pager.total = data.totalCount
    })
  }
  reset() {
    this.searchName = ''
    this.StorehouseService.getWarehouses(this.partClassId, null, this.searchName, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.displayTableData = data.items
      this.pager.total = data.totalCount
    })
  }
  searchWarehouseCategory($event: any) {
    this.searchWarehouseCategoryValue = $event.target.value;
    if (this.searchWarehouseCategoryValue === '') {
      // 如果用户输入的值为空，显示原先的数据
      this.displayTreeData = this.displayTreeData = [{ warehouseCategoryName: '全部', warehouseCategoryId: null }, ...this.treeData];
    } else {
      // 否则，显示过滤后的数据
      this.filteredTreeNodes = this.filterCategories(this.treeData, this.searchWarehouseCategoryValue);
      this.displayTreeData = this.filteredTreeNodes;
      this.displayTreeData = this.displayTreeData.map(this.setOpenTrue);
    }
  }//搜索仓库类目
  afterTreeInit($event: any) {
    if ($event["null"]) {
      $event["null"].data.isActive = true
    }
  }//树形菜单初始化
  nodeSelected($event: any) {
    this.option = $event.data.originItem
    this.partClassId = $event.data.originItem.warehouseCategoryId;
    this.StorehouseService.getWarehouses(this.partClassId, null, this.searchName, this.pager.pageIndex, this.pager.pageSize).subscribe((data: any) => {
      this.displayTableData = data.items
      this.pager.total = data.totalCount
    })
  }//树形菜单点击事件
  filterCategories(categories: any[], keyword: string): any[] {
    const result: any[] = [];
    for (const category of categories) {
      if (category.warehouseCategoryName.includes(keyword)) {
        result.push(category);
      }
      if (category.subWarehouseCategories.length > 0) {
        const filteredSubCategories = this.filterCategories(category.subWarehouseCategories, keyword);
        if (filteredSubCategories.length > 0) {
          result.push(...filteredSubCategories);
        }
      }
    }
    return result;
  }
  setOpenTrue = (node: { open: boolean; subWarehouseCategories: any[]; }) => {
    // 为当前节点添加 open: true 属性
    node.open = true;
    // 如果当前节点有子节点，递归处理子节点
    if (Array.isArray(node.subWarehouseCategories)) {
      node.subWarehouseCategories = node.subWarehouseCategories.map(this.setOpenTrue);
    }
    return node;
  }
}
